package com.fz.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Created by webrx on 2018-06-27.
 */
public class AuthInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest req, HttpServletResponse res, Object o) throws Exception {
        boolean flag = true;
        HttpSession s = req.getSession();
        if(s.getAttribute("admin")==null){
            flag = false;
            //转发跳转
            req.getRequestDispatcher("/admin/login").forward(req,res);
            //res.sendRedirect("/admin/login");
        }
        return flag;
    }

    @Override
    public void postHandle(HttpServletRequest req, HttpServletResponse res, Object o, ModelAndView mv) throws Exception {
    }
    @Override
    public void afterCompletion(HttpServletRequest req, HttpServletResponse res, Object o, Exception e) throws Exception {
    }
}
